import URL from './url.js'
import base64 from 'base-64'
import {Alert} from 'react-native';
export default class Message{


    //消息中签到
 async messageUserSignIn(userID,interactionCourseID) {
        try{
            let response = await fetch(URL.userSignIn_URL, {
                method: 'GET',
                headers: {
                    'userID':userID, 
                    'interactionCourseID': interactionCourseID, 
                }
            });
            return response;
        }catch (error) {
            console.log("User", error);
        }
        return null;
    }


    // messageUserSignIn(userID,interactionCourseID) {
    //     let keymap = {
    //         method: 'GET',
    //         headers: { 'interactionCourseID': interactionCourseID, 'userID':userID, },
    //     };
    // return new Promise((resolve, reject) => {
    //    fetch(URL.userSignIn_URL, keymap).then(
    //         (result) => {
    //             console.log(result);
    //             result.text().then(
    //                 (resultData) => {
    //                     console.log('resultDate',resultData);
    //                     resolve(resultData);
    //                     Alert.alert('温馨提醒:',resultData);
    //                 }
    //             ).catch(
    //                 (error) => {
    //                     console.log(error);
    //                     reject(error);
    //                 }
    //                 )   
    //         }
    //     ).catch((error) => { console.log('error:' + error); });
    //     });
    // }

    getMessageList(userid,type) {
        let keymap = {
            method: 'GET',
            headers: { 'receiveid': userid,'type':type},
        };
        return new Promise((resolve, reject) => {
            fetch(URL.getMessageList_url, keymap).then((result) => {//URL.COURSE_INFO_URL
                result.json().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })
        });

    }

    getUnreadMessageNum(userid,type,readstatus) {
        let keymap = {
            method: 'GET',
            headers: { 'receiveid': userid,'type':type,'readstatus':readstatus},
        };
        return new Promise((resolve, reject) => {
            fetch(URL.getMessageNum_url, keymap).then((result) => {//URL.COURSE_INFO_URL
                result.text().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })
        });

    }

    //设置消息已读
    updateReadStatus(receiveid) {
        let keymap = {
            method: 'POST',
            headers: { 'Content-Type':'application/json','receiveid': receiveid},
        };
        return new Promise((resolve, reject) => {
            fetch(URL.updateReadStatus_url, keymap).then((result) => {
                result.text().then(
                    (resultData) => {
                        resolve(resultData);
                    }
                ).catch(
                    (error) => { reject(error); })
            }).catch((err) => { reject(err) })
        });

    }

}